/**
 * 
 */
package org.millioncall.yueyoga.admin.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.millioncall.openplatform.common.PaginateParams;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.common.StartLimitPairImpl;
import org.millioncall.openplatform.dao.PreparedQuery;
import org.millioncall.yueyoga.admin.dao.BaseDao;
import org.millioncall.yueyoga.admin.dao.ConsumptionDAO;
import org.millioncall.yueyoga.admin.model.Consume;
import org.millioncall.yueyoga.admin.model.Consumption;
import org.springframework.stereotype.Repository;

/**
 * @author lisj Mar 15, 2014 11:54:21 AM
 */
@Repository
public class ConsumptionDAOImpl extends BaseDao implements ConsumptionDAO {
	private static final String SAVE_CONSUMPTION = "saveConsumption";
	private static final String FIND_CONSUMPTION_BY_ID = "findConsumptionById";
	private static final String FIND_CONSUMPTIONS_BY_CONDITIONS = "findConsumptionsByConditions";

	@Override
	public boolean saveConsumption(Map<String, Object> map) {
		try {
			this.getPagableSqlSessionTemplate().insert(SAVE_CONSUMPTION, map);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public Consumption findConsumptionById(Integer id) {
		Consumption consumption = this.getPagableSqlSessionTemplate()
				.selectOne(FIND_CONSUMPTION_BY_ID, id);
		return consumption;
	}

	@Override
	public PaginatedList<Consumption> findConsumptionsInPage(
			Map<String, Object> map, int start, int limit) {
		PreparedQuery<Consumption> pq = this.getPagableSqlSessionTemplate()
				.findAll(FIND_CONSUMPTIONS_BY_CONDITIONS, map);

		PaginatedList<Consumption> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));

		if (pl.isEmpty()) {
			return null;
		} else {
			return pl;
		}
	}

	@Override
	public boolean saveConsume(Consume consume) {
		int i = this.getPagableSqlSessionTemplate().insert("saveConsume",
				consume);
		if (i > 0)
			return true;
		return false;
	}

	@Override
	public boolean isConsumed(Map<String, Object> map) {
		PreparedQuery<Consume> pq = this.getPagableSqlSessionTemplate()
				.findAll("isConsumed", map);
		List<Consume> list = pq.list();
		if (list.size() > 0)
			return true;
		return false;
	}

	@Override
	public Consume findConsumptionByCdts(Integer memberId, Integer scheduleId,
			String date) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", memberId);
		map.put("scheduleId", scheduleId);
		map.put("date", date);
		List<Consume> list = this.getPagableSqlSessionTemplate().selectList(
				"findConsumptionByCdts", map);
		if (list == null || list.isEmpty()) {
			return null;
		} else {
			return list.get(0);
		}
	}

	@Override
	public int deleteConsumeById(Integer consumeId) {
		return this.getPagableSqlSessionTemplate().delete("deleteConsumeById",
				consumeId);
	}
}
